import express from 'express'
import cors from 'cors'

const app = express()
const PORT = 3000

app.use(cors())
app.use(express.json())

// 模拟商品数据
const products = [
  // 美妆
  { id: 1, name: '保湿面膜', category: 'makeup', price: 89, image: 'https://picsum.photos/200?random=1', description: '深层补水，滋养肌肤' },
  { id: 2, name: '口红套装', category: 'makeup', price: 199, image: 'https://picsum.photos/200?random=2', description: '多色可选，持久不脱色' },
  { id: 3, name: '洁面乳', category: 'makeup', price: 69, image: 'https://picsum.photos/200?random=3', description: '温和清洁，不紧绷' },
  { id: 19, name: 'NARS大白饼', category: 'makeup', price: 399, image: 'https://images.unsplash.com/photo-1522337360788-8b13dee7a37e?w=400', description: 'NARS Light Reflecting Pressed Setting Powder 定妆粉饼，透明无痕，长效控油，打造清透妆感' },
  
  // 服饰
  { id: 4, name: '春季连衣裙', category: 'clothing', price: 299, image: 'https://picsum.photos/200?random=4', description: '时尚百搭，舒适面料' },
  { id: 5, name: '休闲T恤', category: 'clothing', price: 79, image: 'https://picsum.photos/200?random=5', description: '纯棉材质，透气舒适' },
  { id: 6, name: '牛仔裤', category: 'clothing', price: 159, image: 'https://picsum.photos/200?random=6', description: '经典款式，修身剪裁' },
  
  // 数码
  { id: 7, name: '蓝牙耳机', category: 'digital', price: 399, image: 'https://picsum.photos/200?random=7', description: '降噪功能，续航持久' },
  { id: 8, name: '智能手表', category: 'digital', price: 1299, image: 'https://picsum.photos/200?random=8', description: '健康监测，运动追踪' },
  { id: 9, name: '移动电源', category: 'digital', price: 99, image: 'https://picsum.photos/200?random=9', description: '大容量，快充技术' },
  
  // 电器
  { id: 10, name: '电饭煲', category: 'appliance', price: 299, image: 'https://picsum.photos/200?random=10', description: '智能预约，24小时保温' },
  { id: 11, name: '空气净化器', category: 'appliance', price: 899, image: 'https://picsum.photos/200?random=11', description: '高效过滤，静音运行' },
  { id: 12, name: '榨汁机', category: 'appliance', price: 199, image: 'https://picsum.photos/200?random=12', description: '一键操作，易清洗' },
  
  // 生鲜
  { id: 13, name: '新鲜苹果', category: 'fresh', price: 29.9, image: 'https://picsum.photos/200?random=13', description: '脆甜多汁，产地直供' },
  { id: 14, name: '有机蔬菜', category: 'fresh', price: 19.9, image: 'https://picsum.photos/200?random=14', description: '绿色无公害，营养丰富' },
  { id: 15, name: '进口牛肉', category: 'fresh', price: 159, image: 'https://picsum.photos/200?random=15', description: '精选部位，新鲜配送' },
  
  // 户外
  { id: 16, name: '登山杖', category: 'outdoor', price: 149, image: 'https://picsum.photos/200?random=16', description: '轻便耐用，防滑设计' },
  { id: 17, name: '帐篷', category: 'outdoor', price: 599, image: 'https://picsum.photos/200?random=17', description: '防风防雨，易搭建' },
  { id: 18, name: '露营灯', category: 'outdoor', price: 89, image: 'https://picsum.photos/200?random=18', description: 'LED灯珠，长续航' }
]

// 获取热门商品
app.get('/api/products/hot', (req, res) => {
  // 选择一些热门商品，包括NARS大白饼
  const hotProductIds = [19, 1, 4, 7, 10, 2, 16, 13] // 包含NARS大白饼和其他商品
  const hotProducts = hotProductIds
    .map(id => products.find(p => p.id === id))
    .filter(Boolean)
    .map(product => ({
      ...product,
      category: product.category
    }))
  res.json(hotProducts.slice(0, 8))
})

// 根据分类获取商品
app.get('/api/products', (req, res) => {
  const { category } = req.query
  let filteredProducts = products
  
  if (category && category !== 'all') {
    filteredProducts = products.filter(p => p.category === category)
  }
  
  res.json(filteredProducts)
})

// 获取单个商品详情
app.get('/api/products/:id', (req, res) => {
  const { id } = req.params
  const product = products.find(p => p.id === parseInt(id))
  
  if (!product) {
    return res.status(404).json({ message: '商品未找到' })
  }
  
  res.json(product)
})

// 添加到购物车
app.post('/api/cart', (req, res) => {
  const { productId } = req.body
  const product = products.find(p => p.id === productId)
  
  if (!product) {
    return res.status(404).json({ message: '商品未找到' })
  }
  
  res.json({ message: '已添加到购物车', product })
})

// 搜索商品
app.get('/api/search', (req, res) => {
  const { keyword } = req.query
  
  if (!keyword) {
    return res.json([])
  }
  
  const results = products.filter(p => 
    p.name.includes(keyword) || 
    p.description?.includes(keyword)
  )
  
  res.json(results)
})

// 模拟用户数据
const users = [
  { id: 1, username: 'admin', password: '123456', email: 'admin@example.com' },
  { id: 2, username: 'test', password: '123456', email: 'test@example.com' }
]

// 用户注册
app.post('/api/register', (req, res) => {
  const { username, password, email } = req.body
  
  // 验证输入
  if (!username || !password || !email) {
    return res.json({ success: false, message: '请填写完整信息' })
  }
  
  // 检查用户名是否已存在
  if (users.find(u => u.username === username)) {
    return res.json({ success: false, message: '用户名已存在' })
  }
  
  // 检查邮箱是否已存在
  if (users.find(u => u.email === email)) {
    return res.json({ success: false, message: '邮箱已被注册' })
  }
  
  // 创建新用户
  const newUser = {
    id: users.length + 1,
    username,
    password, // 实际项目中应该加密
    email
  }
  
  users.push(newUser)
  
  res.json({ 
    success: true, 
    message: '注册成功',
    user: {
      id: newUser.id,
      username: newUser.username,
      email: newUser.email
    }
  })
})

// 用户登录
app.post('/api/login', (req, res) => {
  const { username, password } = req.body
  
  if (!username || !password) {
    return res.json({ success: false, message: '请输入用户名和密码' })
  }
  
  // 查找用户
  const user = users.find(u => u.username === username && u.password === password)
  
  if (!user) {
    return res.json({ success: false, message: '用户名或密码错误' })
  }
  
  // 生成token（实际项目中应该使用JWT等）
  const token = 'token_' + Date.now() + '_' + user.id
  
  res.json({
    success: true,
    message: '登录成功',
    token,
    user: {
      id: user.id,
      username: user.username,
      email: user.email
    }
  })
})

app.listen(PORT, () => {
  console.log(`🚀 服务器运行在 http://localhost:${PORT}`)
})

